/*
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||Thank You For Downloading|||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||This is another Wunce addon||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/
util.PrecacheSound("vo/npc/male01/pain08.wav") --I'll add more later
util.PrecacheSound("vo/npc/male01/moan04.wav")
util.PrecacheSound("items/suitchargeno1.wav")

Thruster_Sound = Sound("PhysicsCannister.ThrusterLoop")
-- BASICALLY if you want to change the health of each limb, here is where to do it. It will screw stuff up (ie speed) so don't do it
maxlimb = {}
for group = 0,10 do
		if group == HITGROUP_LEFTARM or group == HITGROUP_RIGHTARM or group == HITGROUP_LEFTLEG or group == HITGROUP_RIGHTLEG then
			maxlimb[group] = 25
		elseif group == HITGROUP_CHEST then
			maxlimb[group] = 60
		elseif group == HITGROUP_HEAD then
			maxlimb[group] = 10
		end
	end

function PlayerLD( ply ) --Thanks to the wiki for this section. It assigns each body part health
	if ply.AlreadyRagdolled == false then
	ply.hitgroups = {}
	for group = 0,10 do
		if group == HITGROUP_LEFTARM or group == HITGROUP_RIGHTARM or group == HITGROUP_LEFTLEG or group == HITGROUP_RIGHTLEG then
			ply.hitgroups[group] = maxlimb[group]
		elseif group == HITGROUP_CHEST then
			ply.hitgroups[group] = maxlimb[group]
		elseif group == HITGROUP_HEAD then
			ply.hitgroups[group] = maxlimb[group]
		end
	end
	else

		ply:StripWeapons()
		ply:RemoveAllAmmo()
		ply:SetSuppressPickupNotices ( true ) -- thank you to the guy who made Ragmod for this. I didn't even know it existed
		for k, q in pairs(ply.gunn) do
		ply:Give(q)
		end
		for k, wep in pairs (ply:GetWeapons()) do
		wep:SetClip1(0)
		wep:SetClip2(0)
		end
		timer.Create(string.format( "AmmoSet:%s", ply ),0.1 , 1, function()
		for k, q in pairs(ply.SAmmo) do
		
			ply:SetAmmo( q , ply.AmmoName[k] )
			ply:SelectWeapon( ply.curgunn )
		end
		end)
	
	ply:SetSuppressPickupNotices ( false )
	ply.AlreadyRagdolled = false
end
end
hook.Add("PlayerLoadout", "PlayerLD", PlayerLD )
 
 
function ResetValues(ply)
if ply.AlreadyRagdolled == true then	
		ply:SetPos( ply.doll:GetPos() + Vector( 0, 0, 15 ) )
		ply.doll:Remove()
		ply:SetHealth(ply.tempH)
		ply:SetArmor(ply.tempA)
		ply:SetNoDraw( false )
		timer.Create("PostSpawn", 0.1 , 1 , TimerPostSpawn, ply )
else	
ply.AlreadyRagdolled = false
ply.ForceKills = 0
ply:SetNoDraw( false )
ply:ConCommand("-duck")
ply.FirstAidSpray = 2
timer.Create("PostSpawn", 0.1 , 1 , TimerPostSpawn, ply ) -- I'll make the functions
ply.SlowBleedOut = false
ply.BleedTime = CurTime()
end
ply:SetNWEntity("raggy",NULL)
end
hook.Add("PlayerSpawn", "ResetValues", ResetValues)

function TimerPostSpawn(ply)
		if ply:Alive() then
		LegDamage(ply, ply.hitgroups[HITGROUP_LEFTLEG] , ply.hitgroups[HITGROUP_RIGHTLEG] )
		ArmDamage(ply, ply.hitgroups[HITGROUP_LEFTARM] , ply.hitgroups[HITGROUP_RIGHTARM])
		headandbodydamage(ply, ply.hitgroups[HITGROUP_HEAD] , ply.hitgroups[HITGROUP_CHEST])
		SpraySend(ply, ply.FirstAidSpray)			
			ply:SetNWEntity("raggy",nil)	
				ply.curgunn = ply:GetActiveWeapon()
end
end


function MYLIMBS( ply, limb, dmginfo ) 
if ply:Health() < 0 and ply.AlreadyRagdolled == false then ply:Kill() end
local a = dmginfo:GetDamageForce()
local mag = math.sqrt((a.x * a.x) + (a.y * a.y) + (a.z *a.z))


	if mag > 10000 and ply:Alive() and ply.AlreadyRagdolled == false then --antlion guards and explosives can achieve over 10000 easily
				
			savestuff(ply)
			ragdollplayer(ply, dmginfo:GetDamageForce())
				ply.tempH = ply:Health() - dmginfo:GetBaseDamage() 
				ply.tempA = ply:Armor()
			ply.waittimer = CurTime() + ( 10 - (ply.hitgroups[HITGROUP_CHEST]/10)) --if your chest is hurting, getting up is more difficult
	end
	

	
		if dmginfo:IsDamageType( DMG_BLAST ) then --to give explosions the "fuck that hurt" feeling
				if ply:Alive() then
				ply.hitgroups[HITGROUP_CHEST] = ply.hitgroups[HITGROUP_CHEST] - (dmginfo:GetBaseDamage()/3)
				local randy = FindAttackLocation(ply, dmginfo)
				if randy == 3 then
				
				ply.hitgroups[HITGROUP_RIGHTLEG] = ply.hitgroups[HITGROUP_RIGHTLEG] - (dmginfo:GetBaseDamage()/4)
				ply.hitgroups[HITGROUP_RIGHTARM] = ply.hitgroups[HITGROUP_RIGHTARM] - (dmginfo:GetBaseDamage()/4)
				
				elseif randy == 2 then
				ply.hitgroups[HITGROUP_RIGHTLEG] = ply.hitgroups[HITGROUP_RIGHTLEG] - (dmginfo:GetBaseDamage()/8)
				ply.hitgroups[HITGROUP_RIGHTARM] = ply.hitgroups[HITGROUP_RIGHTARM] - (dmginfo:GetBaseDamage()/8)
				ply.hitgroups[HITGROUP_LEFTLEG] = ply.hitgroups[HITGROUP_LEFTLEG] - (dmginfo:GetBaseDamage()/8)
				ply.hitgroups[HITGROUP_LEFTARM] = ply.hitgroups[HITGROUP_LEFTARM] - (dmginfo:GetBaseDamage()/8)
				elseif randy == 1 then
				ply.hitgroups[HITGROUP_LEFTLEG] = ply.hitgroups[HITGROUP_LEFTLEG] - (dmginfo:GetBaseDamage()/4)
				ply.hitgroups[HITGROUP_LEFTARM] = ply.hitgroups[HITGROUP_LEFTARM] - (dmginfo:GetBaseDamage()/4)
				
				
				end
			end
		end
		
		
	if (ply.hitgroups and ply.hitgroups[limb]) and ply:Armor() == 0 then
		if limb == HITGROUP_STOMACH then
		ply.hitgroups[HITGROUP_CHEST] = ply.hitgroups[HITGROUP_CHEST] - dmginfo:GetDamage() --bundle chest with stomach
		else
		
		ply.hitgroups[limb] = ply.hitgroups[limb] - (dmginfo:GetDamage()*2) 
		if ply.hitgroups[limb] <= 0 then
		ply:SetHealth(ply:Health() + ply.hitgroups[limb]) -- this means you take double damage from a red limb
		ply.hitgroups[limb] = 0
		ply.SlowBleedOut = true
		else
		
		end
		end
	end
	
		LegDamage(ply,ply.hitgroups[HITGROUP_LEFTLEG], ply.hitgroups[HITGROUP_RIGHTLEG])
		headandbodydamage(ply, ply.hitgroups[HITGROUP_HEAD], ply.hitgroups[HITGROUP_CHEST])
		ArmDamage(ply, ply.hitgroups[HITGROUP_LEFTARM], ply.hitgroups[HITGROUP_RIGHTARM])
		
		
		if (ply.hitgroups[HITGROUP_LEFTARM]==0 or ply.hitgroups[HITGROUP_RIGHTARM]==0) and ply:GetActiveWeapon() != NULL and ply:GetActiveWeapon():GetHoldType() != "pistol" then
			local oneswap = true
				for k, wep in pairs(ply:GetWeapons()) do
					if wep:GetHoldType() == "pistol" and oneswap ==true then
					ply:SelectWeapon(wep:GetClass())
					end
				end
			end
		
		end
hook.Add("ScalePlayerDamage", "MYLIMBS", MYLIMBS )

function ZombieCount(ply, inf, att, amt, dmginfo)
if ply:IsPlayer() then
local randlimb = HITGROUP_CHEST
if dmginfo:IsDamageType (DMG_SLASH) or dmginfo:IsDamageType( DMG_CLUB ) then
	if att:GetClass() == "npc_antlionguard" then	
		randlimb = HITGROUP_CHEST 
		--dmginfo:ScaleDamage(1)
	elseif att:GetClass() == "npc_headcrab" or att:GetClass() == "npc_headcrab_fast" or att:GetClass() == "npc_headcrab_black" then
		randlimb = HITGROUP_HEAD
		dmginfo:ScaleDamage(0.65)
				if ply.hitgroups[HITGROUP_HEAD] == 0 then
					if att:GetClass() == "npc_headcrab" then
						HeadHumped(ply, att, "npc_zombie")
					elseif att:GetClass() == "npc_headcrab_fast" then
						HeadHumped(ply, att, "npc_fastzombie")
					elseif att:GetClass() == "npc_headcrab_black" then
						HeadHumped(ply, att, "npc_poisonzombie")
					end
				end
				
	elseif att:GetClass() == "npc_antlion" or att:GetClass() == "npc_zombie_torso" then
		if ply:Alive() then
		dmginfo:ScaleDamage(0.5)
			local randy = FindAttackLocation(ply, dmginfo)
				if randy == 3 then
				randlimb = HITGROUP_RIGHTLEG
				elseif randy == 2 then
				randlimb = HITGROUP_CHEST --well technically pelvis or the top of a leg but I stand by this code
				elseif randy == 1 then
				randlimb = HITGROUP_LEFTLEG
				end
			end
	else
	if ply:Alive() then
	dmginfo:ScaleDamage(0.5)
		local randy = FindAttackLocation(ply, dmginfo)
		if randy == 3 then
			randlimb = HITGROUP_RIGHTARM
		elseif randy == 2 then
			randlimb = HITGROUP_CHEST
		elseif randy == 1 then
			randlimb = HITGROUP_LEFTARM
		end
	end
	end
	end

MYLIMBS(ply, randlimb ,dmginfo)

end
end
hook.Add("EntityTakeDamage", "ZombieCount", ZombieCount) 

function LegBreaker( ply, speed )
	ply.hitgroups[HITGROUP_LEFTLEG] = ply.hitgroups[HITGROUP_LEFTLEG] - (speed/40)
	ply.hitgroups[HITGROUP_RIGHTLEG] = ply.hitgroups[HITGROUP_RIGHTLEG] - (speed/40)
	if ply.hitgroups[HITGROUP_LEFTLEG] < 0 then ply.hitgroups[HITGROUP_LEFTLEG] = 0 end
	if ply.hitgroups[HITGROUP_RIGHTLEG] < 0 then ply.hitgroups[HITGROUP_RIGHTLEG] = 0 end
	LegDamage(ply, ply.hitgroups[HITGROUP_LEFTLEG],  ply.hitgroups[HITGROUP_RIGHTLEG])
	savestuff(ply)
			ply.tempH = ply:Health() - speed/40
			ply.tempA = ply:Armor()
	ragdollplayer(ply, Vector(0,0,speed))
	ply.waittimer = CurTime() + ( 10 - (ply.hitgroups[HITGROUP_CHEST]/10))
	
end
hook.Add("GetFallDamage","LegBreaker",LegBreaker)

///////////////////////////////////
///-----Think Section-----------///
///////////////////////////////////

function GetUpThink()
for k, ply in pairs (player.GetAll()) do

	if  ply.AlreadyRagdolled == true and ply:Alive() and ply.doll != nil and ply.zombie == nil then
	local dollspeed = math.sqrt((ply.doll:GetVelocity().z*ply.doll:GetVelocity().z)+(ply.doll:GetVelocity().x*ply.doll:GetVelocity().x)+(ply.doll:GetVelocity().y*ply.doll:GetVelocity().y))
		if ply.waittimer and ply.waittimer < CurTime() and dollspeed < 5 and dollspeed > -5  then
					ply:SetNoTarget( false )
					ply:SetParent()
					ply:Spawn()
					ply:EmitSound("vo/npc/male01/moan04.wav")

		end
	end
	
	if ply:KeyDown( IN_ATTACK ) then --I think "key" thingys are broken. Yes they are, but only on clientside for multiplayer
	ply.spazon = true
	else
	ply.spazon = false
	end
	SendSpazStatus(ply)
	

	if ply.BleedTime and ply.BleedTime < CurTime() and ply.SlowBleedOut == true and ply.zombie == nil and ply.AlreadyRagdolled == false then
	ply:SetHealth(ply:Health() - 5 )
	for group=1,7 do
	if group != 3 and ply.hitgroups[group] > 0 then
	ply.hitgroups[group] = ply.hitgroups[group] -math.ceil(0.05*maxlimb[group]) -- So that you can see yourself deteriorating
		
	end
	end
	LegDamage(ply,ply.hitgroups[HITGROUP_LEFTLEG], ply.hitgroups[HITGROUP_RIGHTLEG])
	headandbodydamage(ply, ply.hitgroups[HITGROUP_HEAD], ply.hitgroups[HITGROUP_CHEST])
	ArmDamage(ply, ply.hitgroups[HITGROUP_LEFTARM], ply.hitgroups[HITGROUP_RIGHTARM])
	ply.BleedTime = CurTime() + math.random(5,10)
		if ply:Health() <= 0 then
		
			if ply.ForceKills < 1 then
				ply:Kill()
				ply.ForceKills = 1
			end
			
		ply.SlowBleedOut = false
		end
	end
	
	if ply.BleedTime and ply.BleedTime < CurTime() and ply.zombie != nil then
		ply.zombie:Spawn()

		ply:Spectate( OBS_MODE_CHASE )
		ply:SpectateEntity( ply.zombie )
		ply:SetParent( ply.zombie )
		
				
		ply.zombie = nil
		ply.AlreadyRagdolled = false
		if ply.doll:IsValid() then
			ply.doll:Remove()
		end
			timer.Create(string.format("zombeh%s",ply:Nick()),5,1, function()
			ply:SetNoTarget( false )
			ply:SetParent()
			ply:KillSilent()
			ply:Spawn()
		end)
	end
	
	if ply.curgunn != nil and ply:GetActiveWeapon() != NULL then
	if ply:GetActiveWeapon():GetClass() != ply.curgunn then 
	
			if (ply.hitgroups[HITGROUP_LEFTARM]==0 or ply.hitgroups[HITGROUP_RIGHTARM]==0) and ply:GetActiveWeapon():GetHoldType() != "pistol" then
				ply:SelectWeapon(ply.curgunn)
			end
	ply.curgunn = ply:GetActiveWeapon():GetClass()
	ClipSize(ply,ply:GetActiveWeapon())
	end
	end
	end
end
hook.Add("Think", "GetUpThink" , GetUpThink)



function FirstAid(ply, key )
///////////////////////////////////
///		Medical Aid Section!    ///
///////////////////////////////////
if key == IN_USE then
local pos = ply:GetShootPos()
local ang = ply:GetAimVector()
local tracedata = {}
tracedata.start = pos
tracedata.endpos = pos+(ang*80)
tracedata.filter = ply
local trace = util.TraceLine(tracedata)

	if trace.Hit and trace.Entity:IsPlayer() and ply.FirstAidSpray > 0 then
			ply:ChatPrint(string.format( "You healed %s", trace.Entity:Nick() ))
			local mysound = CreateSound(ply, Thruster_Sound ) -- That lovely thruster sound :P
			mysound:Play()
			timer.Create(string.format( "Spraysound:%s", ply ),2 , 1, function()
			mysound:Stop()
			end)
			
	ply.FirstAidSpray = ply.FirstAidSpray - 1
	SpraySend(ply, ply.FirstAidSpray)

		
local limb = trace.HitGroup
local target = trace.Entity
local tobleed = false -- this should fix bleeding even though your limb has been healed
		
		for group = 1,7 do
		if 	target.hitgroups[group] and group != 3 then
			if group == limb then
			target.hitgroups[group] = target.hitgroups[group]+(maxlimb[group]*4)/5
			else
			target.hitgroups[group] = target.hitgroups[group]+maxlimb[group]/5
			end
			if target.hitgroups[group] > maxlimb[group] then target.hitgroups[group] = maxlimb[group] end
		end
		end
		trace.Entity.SlowBleedOut = false
	
LegDamage(target,target.hitgroups[HITGROUP_LEFTLEG], target.hitgroups[HITGROUP_RIGHTLEG])
headandbodydamage(target, target.hitgroups[HITGROUP_HEAD], target.hitgroups[HITGROUP_CHEST])
ArmDamage(target, target.hitgroups[HITGROUP_LEFTARM],target.hitgroups[HITGROUP_RIGHTARM])
		
trace.Entity:SetHealth(trace.Entity:Health() + 40) 
if trace.Entity:Health() > trace.Entity:GetMaxHealth() then trace.Entity:SetHealth(trace.Entity:GetMaxHealth()) end
end
end
end
hook.Add("KeyPress", "FirstAid", FirstAid)


function LegDamage(ply, Lleg, Rleg)
	if Lleg > 0 and Rleg > 0 then
		ply:SetWalkSpeed((Lleg+ Rleg)*1.6+20)
		ply:SetRunSpeed((5 *(Lleg+ Rleg)) +50)
		ply:ConCommand("-duck")
	else
		ply:ConCommand("+duck")
		ply:SetWalkSpeed(1)
		ply:SetRunSpeed(2)
	end
		
		umsg.Start("LeftLeg", ply)
		if Lleg > 0 then
		umsg.Float(Lleg)
		else
		umsg.Float(0)
		ply.SlowBleedOut = true
		end
		umsg.End()
		umsg.Start("RightLeg", ply)
		if Rleg > 0 then
		umsg.Float(Rleg)
		else
		umsg.Float(0)
		ply.SlowBleedOut = true
		end
		umsg.End()
end

function SpraySend(ply, cans)

		umsg.Start("SprayAmt", ply)
		umsg.Float(cans)
		umsg.End()


end




function ArmDamage(ply, Larm, Rarm)
		
		umsg.Start("LeftArm", ply)
		if Larm > 0 then
		umsg.Float(Larm)
		else
		umsg.Float(0)
		end
		umsg.End()

		umsg.Start("RightArm", ply)
		if Rarm > 0 then
		umsg.Float(Rarm)
		else
		umsg.Float(0)
		end
		umsg.End()
		
end

function headandbodydamage(ply, head, body)
		umsg.Start("Head", ply)
		if head > 0 then
		umsg.Float(head)
		else
		umsg.Float(0)
		end
		umsg.End()
		umsg.Start("Body", ply)
		if body > 0 then
		umsg.Float(body)
		else
		umsg.Float(0)
		end
		umsg.End()
end

function Ragdolldmg( ent, inflictor, attacker, amount, dmginfo )
for k, ply in pairs(player.GetAll()) do
if ply.zombie == nil then
	if ent == ply.doll and ply.AlreadyRagdolled == true and ply:Alive() then
			if dmginfo:IsDamageType(DMG_CRUSH) or dmginfo:IsDamageType(DMG_FALL) then
			ply.tempH = ply.tempH - (dmginfo:GetDamage()/5)
			ply:SetHealth( ply:Health() - (dmginfo:GetDamage()/5))
			ply.hitgroups[HITGROUP_RIGHTLEG] = ply.hitgroups[HITGROUP_RIGHTLEG] - (dmginfo:GetDamage()/10) --this needs to be fixed to actually reflect the locations of damage
			ply.hitgroups[HITGROUP_LEFTLEG] = ply.hitgroups[HITGROUP_LEFTLEG] - (dmginfo:GetDamage()/10)
			else
			ply.tempH = ply.tempH - dmginfo:GetDamage()
			ply:SetHealth( ply:Health() - dmginfo:GetDamage())
			end

LegDamage(ply, ply.hitgroups[HITGROUP_LEFTLEG], ply.hitgroups[HITGROUP_RIGHTLEG])
			if ply.tempH <= 0 and ply:Alive() then
			ply:KillSilent() 
			
			timer.Create(string.format("RagTimer:%s",ply:Nick()), 15, 1, removedoll, ply, ply.doll )
			RagdollDeath(ply)
			end
	end
end
end
end
hook.Add("EntityTakeDamage","Ragdolldmg",Ragdolldmg)


function removedoll(ply,  doll)
if doll:IsValid() then
doll:Remove()
doll = nil
end
if !ply:Alive() then
ply:Spawn()
end
end



function RagdollDeath( ply )
if ply.AlreadyRagdolled == true then
	ply:EmitSound("vo/npc/male01/pain08.wav")
	ply:SetNoTarget( false )
	ply:SetParent()
	ply.AlreadyRagdolled = false
	ply.ForceKills = 1
	end
end
hook.Add("PlayerDeath","RagdollDeath",RagdollDeath)


function SendSpazStatus(ply)
ply:SetNWBool("spazon",ply.spazon)
end


function HeadHumped(ply, att, zombietype)
				ply:StripWeapons()
				ragdollplayer(ply ,Vector(0,0,0))
				ply.zombie = ents.Create(zombietype)
				ply.zombie:SetPos(ply.doll:GetPos()) 
				att:Remove()
				if SHAID then
				npctotalnum = npctotalnum - 1 
				end
				ply.BleedTime = CurTime() + 5
end

////////////////////////////////////
///	   Clip Size Function    	 ///
////////////////////////////////////

function ClipSize(ply, wep)
		local curmax = -1
		if wep.Primary != nil then
		curmax = wep.Primary.ClipSize
		end
		umsg.Start("clipsize", ply)
		umsg.Float(curmax)
		umsg.End()
end
////////////////////////////////////
///		 Ragdoll Function        ///
////////////////////////////////////

function ragdollplayer(ply, Force)
					local ragdoll = ents.Create( "prop_ragdoll" ) --Special thanks to overv for this section... the wiki wasn't very useful
					ragdoll:SetModel( ply:GetModel() )
					ragdoll:SetPos( ply:GetPos() )
					ragdoll:SetAngles( ply:GetAngles() )
					ragdoll:SetCollisionGroup(COLLISION_GROUP_PUSHAWAY)
					ragdoll:Spawn()
					ragdoll:Activate()
					if ply:GetInfoNum("sh_fpdeath") == 0 then
					ply:Spectate( OBS_MODE_CHASE )
					ply:SpectateEntity( ragdoll )
					ply:SetParent( ragdoll )
					end
					local chest =ragdoll:GetPhysicsObjectNum(1)
					chest:ApplyForceCenter(Force)
					ply.AlreadyRagdolled = true
					if ply.doll != nil and ply.doll:IsValid() then
					ply.doll:Remove()
					end
					ply.doll = ragdoll
					ply:SetNWEntity("raggy",ply.doll)
					ply:SetNoDraw( true )
end

////////////////////////////////////
///	   Self Heal Function        ///
////////////////////////////////////
function Self_Heal(ply, c ,argument)
local arg = tonumber(argument[1])
	if ply.FirstAidSpray > 0 then
	ply.FirstAidSpray = ply.FirstAidSpray - 1
	ply.hitgroups[arg] = ply.hitgroups[arg] + (maxlimb[arg]*3)/5
	ply:SetHealth(ply:Health() + 20)
	if ply.hitgroups[arg] > maxlimb[arg] then ply.hitgroups[arg] = maxlimb[arg] end
	
	local mysound = CreateSound(ply, Thruster_Sound ) -- That lovely thruster sound :P
			mysound:Play()
			timer.Create(string.format( "Spraysound:%s", ply ),2 , 1, function()
				mysound:Stop()
			end)
	end
		local stillbleed = false
		for group = 1,7 do
			if group != 3 and ply.hitgroups[group] == 0 then
			stillbleed = true
			end
		end
	ply.SlowBleedOut = stillbleed
	
		LegDamage(ply,ply.hitgroups[HITGROUP_LEFTLEG], ply.hitgroups[HITGROUP_RIGHTLEG])
		headandbodydamage(ply, ply.hitgroups[HITGROUP_HEAD], ply.hitgroups[HITGROUP_CHEST])
		ArmDamage(ply, ply.hitgroups[HITGROUP_LEFTARM], ply.hitgroups[HITGROUP_RIGHTARM])
		SpraySend(ply, ply.FirstAidSpray)	
end
concommand.Add("Self_Heal", Self_Heal)

function savestuff(ply)
ply.gunn = {}
				ply.SAmmo = {}
				ply.AmmoName = {}
				ply.curgunn = ply:GetActiveWeapon():GetClass()
					for k, wep in pairs (ply:GetWeapons()) do
					
						// Save Guns //
						ply.gunn[k] = wep:GetClass()
						
						//Save Primary Ammo //
						if (wep:GetPrimaryAmmoType() == nil or ply:GetAmmoCount(wep:GetPrimaryAmmoType()) == nil ) then 
						ply.SAmmo[2*k] = 0
						ply.AmmoName[2*k] = "GaussEnergy"
						else
						
						ply.SAmmo[2*k] = ply:GetAmmoCount(wep:GetPrimaryAmmoType()) + wep:Clip1()
						ply.AmmoName[2*k] = wep:GetPrimaryAmmoType() 
						end
						
						// Save Secondary Ammo //
						if (wep:GetSecondaryAmmoType() == nil or ply:GetAmmoCount(wep:GetSecondaryAmmoType()) == nil ) then
						ply.SAmmo[(2*k)+1] = 0
						ply.AmmoName[(2*k)+1] = "GaussEnergy"
						else
						ply.SAmmo[(2*k)+1] = ply:GetAmmoCount(wep:GetSecondaryAmmoType()) + wep:Clip2()
						ply.AmmoName[(2*k)+1] = wep:GetSecondaryAmmoType()
						end
						
						
					end
			ply:StripWeapons()
			ply:RemoveAllAmmo()
			ply.AlreadyRagdolled = true
end

///////////////////////////////////////////		I should give this to the good people of facepunch!
///		Super Critical Fucntion			///
///////////////////////////////////////////
function FindAttackLocation(ply, dmginfo) -- I still can't believe I could put my math skills to use

local pi = 3.141592654													--	a
local a = ply:GetAimVector()											--	\	This angle between is found through the equation:
local b = (dmginfo:GetDamageForce()):GetNormalized() 					--	 \	
local theta = math.acos(Vector(a.x,a.y):DotProduct(Vector(b.x,b.y)))	--	  \_______b		arccos(a . b)
local sign = Vector(a.x,a.y):Cross(Vector(b.x,b.y))						--    
if sign.z < 0 and theta > (pi / 6) and theta < (5*pi / 6) then 			-- then the side in which the angle is on is found by the cross product
return 1 -- LEFT SIDE													-- of the x and y components (which produces positive number in the z direction
elseif sign.z > 0 and theta > (pi / 6) and theta < (5*pi / 6) then		-- for anti-clockwise and negative z for clockwise. This is then used to
return 3 -- RIGHT SIDE													-- determine which side the angle is on and therefore where the attack came
else																	-- from. This can be applied to more than just attacks, as long as you have
return 2 -- CENTRE														-- two normalised vectors :D
end
end

